<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="postId" :request-api="listPostApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['system:post:add']" icon="CirclePlus" @click="openDialog(1, '岗位信息新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['system:post:import']" icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['system:post:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button
          type="danger"
          v-auth="['system:user:remove']"
          icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link icon="View" v-auth="['system:post:query']" @click="openDialog(3, '岗位信息查看', scope.row)"> 查看 </el-button>
        <el-button type="primary" link icon="EditPen" v-auth="['system:post:edit']" @click="openDialog(2, '岗位信息编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['system:post:remove']" @click="deletePost(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" :items-options="itemsOptions" :model="model" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="PostManage">
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/FormDialog/index.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import {
  listPostApi,
  delPostApi,
  addPostApi,
  updatePostApi,
  importTemplateApi,
  importDataApi,
  exportApi,
  getPostApi
} from '@/api/modules/system/post'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'))

// ProTable 实例
const proTable = ref<ProTableInstance>()
// 表单model
const model = ref({})
// 删除岗位信息信息
const deletePost = async (params: any) => {
  await useHandleData(delPostApi, params.postId, `删除【${params.postName}】岗位信息`)
  proTable.value?.getTableList()
}

// 批量删除岗位信息信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delPostApi, ids, '删除所选岗位信息信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 导出岗位信息列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出岗位信息数据?', '温馨提示', { type: 'warning' }).then(() =>
    useDownload(exportApi, '岗位信息列表', proTable.value?.searchParam)
  )
}

// 批量添加岗位信息
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '岗位信息',
    tempApi: importTemplateApi,
    importApi: importDataApi,
    getTableList: proTable.value?.getTableList
  }
  dialogRef.value?.acceptParams(params)
}

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  let res = { data: {} }
  if (row?.postId) {
    res = await getPostApi(row?.postId || null)
  }
  model.value = type == 1 ? {} : res.data
  // 重置表单
  setItemsOptions()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    api: type == 1 ? addPostApi : updatePostApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'postId', label: '岗位ID' },
  {
    prop: 'postCode',
    label: '岗位编码',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'postName',
    label: '岗位名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'postSort',
    label: '显示顺序',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'status',
    label: '岗位状态',
    tag: true,
    enum: sys_normal_disable,
    search: {
      el: 'tree-select'
    },
    width: 180
  },
  {
    prop: 'createBy',
    label: '创建者',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'createTime',
    label: '创建时间',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'updateTime',
    label: '更新时间',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'remark',
    label: '备注',
    search: {
      el: 'input'
    },
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
])

// 表单配置项
let itemsOptions = reactive<ProForm.ItemsOptions[]>([])
const setItemsOptions = () => {
  itemsOptions = [
    {
      label: '岗位编码',
      prop: 'postCode',
      rules: [{ required: true, message: '岗位编码不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入岗位编码'
      }
    },
    {
      label: '岗位名称',
      prop: 'postName',
      rules: [{ required: true, message: '岗位名称不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入岗位名称'
      }
    },
    {
      label: '显示顺序',
      prop: 'postSort',
      rules: [{ required: true, message: '显示顺序不能为空', trigger: 'blur' }],
      compOptions: {
        elTagName: 'input-number',
        min: 0
      }
    },
    {
      label: '状态',
      prop: 'status',
      rules: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
      compOptions: {
        elTagName: 'radio-group',
        value: '1',
        enum: sys_normal_disable.value
      }
    },
    {
      label: '备注',
      prop: 'remark',
      compOptions: {
        placeholder: '请输入备注'
      }
    }
  ]
}
</script>
